/**
 * @param {string} password
 * @return {number}
 */
 var strongPasswordChecker = function(password) {
  let lowerLetterCnt = 0;
  let upperLetterCnt = 0;
  let numberCnt = 0;
  let otherCnt = 0;
  password.split('').forEach(a => {
    if (a >= 'a' && a <= 'z') {
      lowerLetterCnt++;
    } else if (a >= 'A' && a <= 'Z') {
      upperLetterCnt++;
    } else if (a >= '0' && a <= '9') {
      numberCnt++;
    } else {
      otherCnt++;
    }
  });
  let ret = 0;
  let length = password.length;
  let s = 0;
  while(s + 2 < password.length) {
    if(password[s] === password[s + 1] && password[s] === password[s + 2]) {
      ret += 1;
      if (lowerLetterCnt === 0) {
        lowerLetterCnt++;
        length += length < 6 ? 1 : 0;
      } else if (upperLetterCnt === 0) {
        upperLetterCnt++;
        length += length < 6 ? 1 : 0;
      } else if (numberCnt === 0) {
        numberCnt++;
        length += length < 6 ? 1 : 0;
      } else {
        if (length  > 20) {
          length--;
        } else if (length < 6) {
          length++;
        }
      }
    }
    s++;
  }
  if(length < 6) {
    ret += (lowerLetterCnt === 5 - otherCnt || upperLetterCnt === 5 - otherCnt || numberCnt === 5 - otherCnt) ? 2 : 6 - length;
  } else if (length > 20) {
    ret += length - 20;
    if (lowerLetterCnt === 0) ret ++;
    if (upperLetterCnt === 0) ret ++;
    if (numberCnt === 0) ret ++;
  } else {
    if (lowerLetterCnt === 0) ret ++;
    if (upperLetterCnt === 0) ret ++;
    if (numberCnt === 0) ret ++;
  }
  return ret;
};

console.log(strongPasswordChecker('aaaB1'))